import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:kxy_flutter_app/r.dart';
import 'package:kxy_flutter_app/view/examination/view_model/test_model.dart';
import 'package:provider/provider.dart';

class TestBottomBarWidget extends StatelessWidget {
  final SwiperController swiperCtrl;
  final Function complete;

  const TestBottomBarWidget({
    Key key,
    @required this.swiperCtrl,
    @required this.complete,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var border = Border(
      top: BorderSide(
        color: Color.fromRGBO(240, 240, 240, 1),
        width: 1,
      ),
    );

    return Consumer<TestModel>(
      builder: (cxt, model, child) {
        return Container(
          height: 50,
          decoration: BoxDecoration(border: border),
          padding: EdgeInsets.symmetric(horizontal: 0),
          child: body(model, context),
        );
      },
    );
  }

  Widget body(TestModel model, BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Expanded(child: btnPre(model, context)),
        Expanded(child: btnCollect(model, context)),
        Builder(builder: (_) {
          if (model.current.confirmed) {
            return Expanded(child: btnNext(model, context));
          }
          return Expanded(child: btnConfirm(model, context));
        }),
      ],
    );
  }

  BoxDecoration decoration() {
    return BoxDecoration(
      color: Colors.white,
      border: Border(
        right: BorderSide(
          color: Color.fromRGBO(240, 240, 240, 1),
        ),
      ),
    );
  }

  TextStyle labelStyle(Color color) {
    return TextStyle(
      fontSize: 14,
      color: color,
      fontWeight: FontWeight.w500,
    );
  }

  Widget btnPre(TestModel model, BuildContext context) {
    var isBegin = model.index == 0;
    return GestureDetector(
      child: Container(
        height: 50,
        decoration: decoration(),
        alignment: Alignment.center,
        child: Text('上一题', style: labelStyle(Colors.black)),
      ),
      onTap: () {
        if (isBegin) return;
        swiperCtrl.previous();
      },
    );
  }

  Widget btnNext(TestModel model, BuildContext context) {
    var isEnd = model.index == model.questionList.length - 1;
    if (isEnd) {
      return btnComplete(model, context);
    }

    return GestureDetector(
      child: Container(
        height: 50,
        color: Colors.white,
        alignment: Alignment.center,
        child: Text('下一题', style: labelStyle(Colors.black)),
      ),
      onTap: () {
        if (isEnd) return;
        swiperCtrl.next();
      },
    );
  }

  Widget btnConfirm(TestModel model, BuildContext context) {
    return GestureDetector(
      child: Container(
        height: 50,
        color: Theme.of(context).primaryColor,
        alignment: Alignment.center,
        child: Text('确认', style: labelStyle(Colors.white)),
      ),
      onTap: () {
        if (model.current.answered) {
          model.submitAnswer();
        }
      },
    );
  }

  Widget btnComplete(TestModel model, BuildContext context) {
    return GestureDetector(
      child: Container(
        height: 50,
        color: Colors.green,
        alignment: Alignment.center,
        child: Text('完成', style: labelStyle(Colors.white)),
      ),
      onTap: complete,
    );
  }

  Widget btnCollect(TestModel model, BuildContext context) {
    var isCollect = model.current.isCollect;
    return GestureDetector(
      child: Container(
        height: 50,
        decoration: decoration(),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.only(bottom: 3),
              child: Image.asset(
                isCollect ? R.assetsCommonCollect : R.assetsCommonUncollect,
                height: 20,
              ),
            ),
            Text(
              '收藏',
              style: TextStyle(
                fontSize: 11,
                color: Colors.black54,
              ),
            ),
          ],
        ),
      ),
      onTap: model.collect,
    );
  }
}
